//
//  XHMyFinancialBenefitsViewController.m
//  umibus
//
//  Created by 冷山 on 16/6/17.
//  Copyright © 2016年 lexingsoft. All rights reserved.
//

#import "XHMyFinancialBenefitsViewController.h"

#import "XHIntegralDetailCell.h"

#import "Services.h"

#import <SVPullToRefresh/SVPullToRefresh.h>

static const NSUInteger kPageSize = 20;
static const NSUInteger kPageInit = 0;

@interface XHMyFinancialBenefitsViewController ()

@property (weak, nonatomic) IBOutlet UITableView *ui_tableView;
@property (strong, nonatomic) NSMutableArray *m_messagesArray;
@property (assign, nonatomic) NSUInteger page;
@property (assign, nonatomic) BOOL hasNextPage;
@property (weak, nonatomic) IBOutlet UILabel *ui_interest;
@property (strong, nonatomic) NSTimer * timer;

@end

@implementation XHMyFinancialBenefitsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.ui_tableView.backgroundColor = [ColorManager colorWithHexString:@"F4F4F4"];
    [self.ui_tableView registerNib:[UINib nibWithNibName:@"XHIntegralDetailCell" bundle:nil] forCellReuseIdentifier:XHIntegralDetailCellIdentifier];
    self.page=kPageInit;
    [self.ui_tableView addInfiniteScrollingWithActionHandler:^{
        if (!self.hasNextPage)
        {
            //[PromptAction showErrorPrompt:@"暂无更多数据"];
        }
        if (self.m_messagesArray.count>=kPageSize){
            [self getUserFinancialBenefits:self.page+1];
            return;
        }
        [self.ui_tableView.infiniteScrollingView stopAnimating];
    }];
    [self.ui_tableView addPullToRefreshWithActionHandler:^{
        self.page=kPageInit;
        [self getUserFinancialBenefits:self.page];
    }];
    [PublicMethod setPullToRefreshView:self.ui_tableView];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    self.page = kPageInit;
    [self getUserFinancialBenefits:self.page];
}

- (void)getUserFinancialBenefits:(NSInteger)page
{
    page = self.page;
    if (!self.m_messagesArray)
    {
        [self showLoadingView];
    }
    [ServiceManager getUserFinancialBenefitsWithPageIndex:page userId:[DataManager sharedInstance].usersId success:^(NSDictionary *data) {
        if (page == kPageInit || self.m_messagesArray == nil)
        {
            self.m_messagesArray = [NSMutableArray array];
        }
        NSDictionary * dic = data[@"result"];
        NSArray * datas = dic[@"list"];
        self.ui_interest.text = [NSString stringWithFormat:@"￥%.2f",[dic[@"totalProfits"] doubleValue]];
        if ([datas isKindOfClass:[NSArray class]])
        {
            if (datas.count>0)
            {
                self.page=page;
                [self.m_messagesArray addObjectsFromArray:datas];
            }
            if (self.m_messagesArray.count ==0)
            {
                self.ui_tableView.hidden = YES;
            }
            else
            {
                self.ui_tableView.hidden = NO;
            }
        }
        else
        {
            
        }
        [self.ui_tableView reloadData];
    
        [self.ui_tableView.infiniteScrollingView stopAnimating];
        [PublicMethod setFinishRefreshView:self.ui_tableView];
        [self performSelector:@selector(stopAnimate:) withObject:nil afterDelay:2.0];
        [self hideLoadingView];

    } failure:^(NSDictionary *data) {
        [self hideLoadingView];
    }];
}

- (void)stopAnimate:(NSTimer*)timer
{
    [self.ui_tableView.pullToRefreshView stopAnimating];
    [PublicMethod setPullToRefreshView:self.ui_tableView];
}


#pragma mark - UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    NSUInteger rows=self.m_messagesArray.count;
    return rows;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = nil;
    cell = [self integralDetailCell:indexPath];
    return cell;
}

- (XHIntegralDetailCell*)integralDetailCell:(NSIndexPath*)indexPath
{
    XHIntegralDetailCell * cell = [self.ui_tableView dequeueReusableCellWithIdentifier:XHIntegralDetailCellIdentifier];
    
    NSDictionary * dic = self.m_messagesArray[indexPath.row];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.ui_getType.text = [NSString stringWithFormat:@"%@",dic[@"operateType"]];
    cell.ui_time.text = [[NSDate dateWithString:dic[@"settleTime"]] stringDateYear];
    cell.ui_money.text = [NSString stringWithFormat:@"%.2f",[dic[@"interest"] doubleValue]];
    cell.ui_addDele.text = @"+";
    return cell;
}

- (CGFloat)tableView:(UITableView*)tableView heightForHeaderInSection:(NSInteger)section
{
    CGFloat height = 0.1;
    return height;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    CGFloat height=60;
    return height;
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
